package org.junzerg.Test;


import com.google.common.base.Joiner;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by pengjunzhe on 2018/4/5.
 */
public class JoinerTest {

    public static void main(String[] args) {

        /*
        连接List列表（join）
         */
        Joiner joiner = Joiner.on(";");
        List<String> list = new ArrayList<String>();
        list.add("123");
        list.add("456");
        list.add("789");
        System.out.println(joiner.join(list));
        // 123;456;789

        /*
        连接Iterator<T>列表
         */
        Iterator<String> iterator = list.iterator();
        System.out.println(joiner.join(iterator));
        // 123;456;789

        /*
        直接连接多个字符串
         */
        System.out.println(joiner.join("123","456","789"));
        // 123;456;789

        /*
        连接字符串和列表（appendTo））
         */
        StringBuilder stringBuilder = new StringBuilder("Normal:");
        System.out.println(joiner.appendTo(stringBuilder,list));
        //Normal:123;456;789

        /*
        处理null
         */
        Joiner joinerForNull = Joiner.on(";").useForNull("_");// 用下划线处理null
        list.add(null);
        list.add("000");
        stringBuilder = new StringBuilder("ForNull:");
        System.out.println(joinerForNull.appendTo(stringBuilder,list));
        // ForNull:123;456;789;_;000

        /*
        跳过null
         */
        Joiner joinerSkipNull = Joiner.on(";").skipNulls();
        stringBuilder = new StringBuilder("SkipNull:");
        System.out.println(joinerSkipNull.appendTo(stringBuilder,list));
        // SkipNull:123;456;789;000
    }

}
